package org.example.backend.Module.School.School.Service.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.example.backend.Module.School.School.Mapper.SchoolMapper;
import org.example.backend.Module.School.School.Pojo.Entity.School;
import org.example.backend.Module.School.School.Service.SchoolService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SchoolServiceImpl implements SchoolService {

    @Resource
    private SchoolMapper schoolMapper;

    @Override
    public List<School> list(String name, String id) {
        LambdaQueryWrapper<School> qw = new LambdaQueryWrapper<>();

        if(StringUtils.isNotBlank(name)) {
            qw.like(School::getName, name);
        }
        if(StringUtils.isNotBlank(id)) {
            qw.like(School::getId, id);
        }

        return schoolMapper.selectList(qw);
    }

    @Override
    public void addOrUpdate(School school) {
        Date now = new Date();
        school.setUpdateTime(now);

        if(school.getId() == null) {
            school.setCreateTime(now);
            schoolMapper.insert(school);
        } else {
            schoolMapper.updateById(school);
        }
    }

    @Override
    public void delete(Long id) {
        schoolMapper.deleteById(id);
    }
}
